<template>
  <view class="device-info-container">
    <!-- 加载状态 -->
    <view v-if="isLoading" class="loading-container">
      <text class="loading-text">加载中...</text>
    </view>

    <!-- 错误状态 -->
    <view v-else-if="error" class="error-container">
      <text class="error-icon">⚠️</text>
      <text class="error-text">{{ error }}</text>
      <button class="retry-btn" @click="loadDeviceInfo">重试</button>
    </view>

    <!-- 设备信息内容 -->
    <view v-else class="info-content">
      <!-- 设备基本信息 -->
      <view class="info-section">
        <view class="section-title">设备信息</view>
        
        <view class="info-item" @click="copyToClipboard(deviceInfo.brand)">
          <view class="info-icon">📱</view>
          <view class="info-label">设备品牌</view>
          <view class="info-value">{{ deviceInfo.brand }}</view>
        </view>

        <view class="info-item" @click="copyToClipboard(deviceInfo.model)">
          <view class="info-icon">📲</view>
          <view class="info-label">设备型号</view>
          <view class="info-value">{{ deviceInfo.model }}</view>
        </view>

        <view class="info-item" @click="copyToClipboard(deviceInfo.system)">
          <view class="info-icon">💻</view>
          <view class="info-label">操作系统</view>
          <view class="info-value">{{ deviceInfo.system }}</view>
        </view>

        <view class="info-item" @click="copyToClipboard(deviceInfo.platform)">
          <view class="info-icon">🖥️</view>
          <view class="info-label">平台类型</view>
          <view class="info-value">{{ deviceInfo.platform }}</view>
        </view>
      </view>

      <!-- 屏幕信息 -->
      <view class="info-section">
        <view class="section-title">屏幕信息</view>
        
        <view class="info-item" @click="copyToClipboard(`${deviceInfo.screenWidth}x${deviceInfo.screenHeight}`)">
          <view class="info-icon">📐</view>
          <view class="info-label">屏幕分辨率</view>
          <view class="info-value">{{ deviceInfo.screenWidth }} x {{ deviceInfo.screenHeight }}</view>
        </view>

        <view class="info-item" @click="copyToClipboard(deviceInfo.pixelRatio.toString())">
          <view class="info-icon">🔍</view>
          <view class="info-label">像素比</view>
          <view class="info-value">{{ deviceInfo.pixelRatio }}</view>
        </view>

        <view class="info-item" @click="copyToClipboard(`${deviceInfo.windowWidth}x${deviceInfo.windowHeight}`)">
          <view class="info-icon">🪟</view>
          <view class="info-label">可用窗口</view>
          <view class="info-value">{{ deviceInfo.windowWidth }} x {{ deviceInfo.windowHeight }}</view>
        </view>

        <view class="info-item" @click="copyToClipboard(deviceInfo.statusBarHeight.toString())">
          <view class="info-icon">📊</view>
          <view class="info-label">状态栏高度</view>
          <view class="info-value">{{ deviceInfo.statusBarHeight }}px</view>
        </view>
      </view>

      <!-- 系统信息 -->
      <view class="info-section">
        <view class="section-title">系统信息</view>
        
        <view class="info-item" @click="copyToClipboard(deviceInfo.language)">
          <view class="info-icon">🌐</view>
          <view class="info-label">系统语言</view>
          <view class="info-value">{{ deviceInfo.language }}</view>
        </view>

        <view class="info-item" @click="copyToClipboard(deviceInfo.version)">
          <view class="info-icon">⚙️</view>
          <view class="info-label">引擎版本</view>
          <view class="info-value">{{ deviceInfo.version }}</view>
        </view>

        <view class="info-item" @click="copyToClipboard(deviceInfo.SDKVersion)">
          <view class="info-icon">🔧</view>
          <view class="info-label">SDK版本</view>
          <view class="info-value">{{ deviceInfo.SDKVersion }}</view>
        </view>

        <view class="info-item" @click="copyToClipboard(deviceInfo.fontSizeSetting.toString())">
          <view class="info-icon">🔤</view>
          <view class="info-label">字体大小</view>
          <view class="info-value">{{ deviceInfo.fontSizeSetting }}</view>
        </view>
      </view>

      <!-- APP专属信息 -->
      <view v-if="deviceInfo.uuid" class="info-section">
        <view class="section-title">设备标识</view>
        
        <view class="info-item" @click="copyToClipboard(deviceInfo.uuid)">
          <view class="info-icon">🔑</view>
          <view class="info-label">设备UUID</view>
          <view class="info-value uuid-value">{{ deviceInfo.uuid }}</view>
        </view>

        <view v-if="deviceInfo.vendor" class="info-item" @click="copyToClipboard(deviceInfo.vendor)">
          <view class="info-icon">🏭</view>
          <view class="info-label">设备厂商</view>
          <view class="info-value">{{ deviceInfo.vendor }}</view>
        </view>

        <view v-if="deviceInfo.isSimulator !== undefined" class="info-item">
          <view class="info-icon">🖥️</view>
          <view class="info-label">模拟器</view>
          <view class="info-value">{{ deviceInfo.isSimulator ? '是' : '否' }}</view>
        </view>
      </view>

      <!-- 存储信息 -->
      <view v-if="deviceInfo.storageInfo" class="info-section">
        <view class="section-title">存储信息</view>
        
        <view class="info-item">
          <view class="info-icon">💾</view>
          <view class="info-label">总容量</view>
          <view class="info-value">{{ formatBytes(deviceInfo.storageInfo.total) }}</view>
        </view>

        <view class="info-item">
          <view class="info-icon">📦</view>
          <view class="info-label">已使用</view>
          <view class="info-value">{{ formatBytes(deviceInfo.storageInfo.used) }}</view>
        </view>

        <view class="info-item">
          <view class="info-icon">📂</view>
          <view class="info-label">可用空间</view>
          <view class="info-value">{{ formatBytes(deviceInfo.storageInfo.free) }}</view>
        </view>

        <!-- 存储使用进度条 -->
        <view class="storage-progress">
          <view class="progress-bar">
            <view 
              class="progress-fill" 
              :style="{ width: storageUsagePercent + '%' }"
            ></view>
          </view>
          <text class="progress-text">已使用 {{ storageUsagePercent }}%</text>
        </view>
      </view>

      <!-- 应用信息 -->
      <view class="info-section">
        <view class="section-title">应用信息</view>
        
        <view class="info-item" @click="copyToClipboard(appInfo.version)">
          <view class="info-icon">📱</view>
          <view class="info-label">应用版本</view>
          <view class="info-value">v{{ appInfo.version }}</view>
        </view>

        <view class="info-item" @click="copyToClipboard(appInfo.platform)">
          <view class="info-icon">🖥️</view>
          <view class="info-label">运行平台</view>
          <view class="info-value">{{ appInfo.platform }}</view>
        </view>

        <view class="info-item" @click="copyToClipboard(appInfo.environment)">
          <view class="info-icon">🌍</view>
          <view class="info-label">运行环境</view>
          <view class="info-value">{{ appInfo.environment }}</view>
        </view>
      </view>

      <!-- 提示信息 -->
      <view class="tip-section">
        <text class="tip-icon">💡</text>
        <text class="tip-text">点击任意信息项可复制到剪贴板</text>
      </view>
    </view>
  </view>
</template>

<script>
import DeviceInfoService from '@/utils/device-info-service.js';

export default {
  data() {
    return {
      isLoading: true,
      error: null,
      deviceInfo: {
        brand: '',
        model: '',
        system: '',
        platform: '',
        screenWidth: 0,
        screenHeight: 0,
        pixelRatio: 0,
        language: '',
        version: '',
        SDKVersion: '',
        windowWidth: 0,
        windowHeight: 0,
        statusBarHeight: 0,
        fontSizeSetting: 0,
        uuid: '',
        vendor: '',
        isSimulator: false,
        storageInfo: null
      },
      appInfo: {
        version: '',
        platform: '',
        environment: ''
      }
    };
  },

  computed: {
    storageUsagePercent() {
      if (!this.deviceInfo.storageInfo) return 0;
      const { total, used } = this.deviceInfo.storageInfo;
      if (total === 0) return 0;
      return Math.round((used / total) * 100);
    }
  },

  onLoad() {
    this.loadDeviceInfo();
  },

  methods: {
    async loadDeviceInfo() {
      this.isLoading = true;
      this.error = null;

      try {
        // 获取设备和应用信息
        const allInfo = await DeviceInfoService.getAllInfo();
        this.deviceInfo = allInfo.device;
        this.appInfo = allInfo.app;

        console.log('设备信息加载成功:', this.deviceInfo);
        console.log('应用信息加载成功:', this.appInfo);
      } catch (error) {
        console.error('加载设备信息失败:', error);
        this.error = error.message || '加载设备信息失败，请重试';
      } finally {
        this.isLoading = false;
      }
    },

    formatBytes(bytes) {
      return DeviceInfoService.formatBytes(bytes);
    },

    copyToClipboard(text) {
      if (!text) {
        uni.showToast({
          title: '无内容可复制',
          icon: 'none',
          duration: 1500
        });
        return;
      }

      uni.setClipboardData({
        data: text.toString(),
        success: () => {
          uni.showToast({
            title: '已复制到剪贴板',
            icon: 'success',
            duration: 1500
          });
        },
        fail: (error) => {
          console.error('复制失败:', error);
          uni.showToast({
            title: '复制失败',
            icon: 'error',
            duration: 1500
          });
        }
      });
    }
  }
};
</script>

<style scoped>
.device-info-container {
  min-height: 100vh;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  padding: 20rpx;
}

/* 加载状态 */
.loading-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 80vh;
}

.loading-text {
  font-size: 28rpx;
  color: #666;
}

/* 错误状态 */
.error-container {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  min-height: 80vh;
  padding: 40rpx;
}

.error-icon {
  font-size: 80rpx;
  margin-bottom: 20rpx;
}

.error-text {
  font-size: 28rpx;
  color: #e74c3c;
  text-align: center;
  margin-bottom: 30rpx;
}

.retry-btn {
  background: #667eea;
  color: white;
  border: none;
  border-radius: 12rpx;
  padding: 20rpx 40rpx;
  font-size: 28rpx;
}

/* 信息内容 */
.info-content {
  padding-bottom: 40rpx;
}

.info-section {
  background: white;
  border-radius: 16rpx;
  box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.1);
  padding: 30rpx;
  margin-bottom: 20rpx;
}

.section-title {
  font-size: 32rpx;
  font-weight: bold;
  color: #2d3436;
  margin-bottom: 20rpx;
  padding-bottom: 16rpx;
  border-bottom: 2rpx solid #f0f0f0;
}

.info-item {
  display: flex;
  align-items: center;
  padding: 20rpx 0;
  border-bottom: 1rpx solid #f8f9fa;
  transition: background-color 0.3s ease;
}

.info-item:last-child {
  border-bottom: none;
}

.info-item:active {
  background-color: #f8f9fa;
}

.info-icon {
  font-size: 32rpx;
  margin-right: 16rpx;
  width: 40rpx;
  text-align: center;
}

.info-label {
  flex: 1;
  font-size: 28rpx;
  color: #636e72;
}

.info-value {
  font-size: 26rpx;
  color: #2d3436;
  font-weight: 500;
  max-width: 400rpx;
  text-align: right;
  word-break: break-all;
}

.uuid-value {
  font-size: 20rpx;
  font-family: monospace;
}

/* 存储进度条 */
.storage-progress {
  margin-top: 20rpx;
  padding-top: 20rpx;
  border-top: 1rpx solid #f0f0f0;
}

.progress-bar {
  width: 100%;
  height: 12rpx;
  background: #f0f0f0;
  border-radius: 6rpx;
  overflow: hidden;
  margin-bottom: 12rpx;
}

.progress-fill {
  height: 100%;
  background: linear-gradient(90deg, #667eea 0%, #764ba2 100%);
  transition: width 0.3s ease;
}

.progress-text {
  font-size: 24rpx;
  color: #636e72;
  display: block;
  text-align: center;
}

/* 提示信息 */
.tip-section {
  background: #fff3cd;
  border-radius: 12rpx;
  padding: 20rpx;
  display: flex;
  align-items: center;
  margin-top: 20rpx;
}

.tip-icon {
  font-size: 28rpx;
  margin-right: 12rpx;
}

.tip-text {
  flex: 1;
  font-size: 24rpx;
  color: #856404;
}
</style>
